//
// Created by hsj on 2022/1/18.
//
#include <iostream>
#include "t_class_extend_virtual.h"
namespace hsj_class_extend_virtual{

    //设置新的，返回原始的
    format setFormat(){
        return cout.setf(std::ios_base::fixed,std::ios_base::floatfield);
    }
    //使用原始的重置
    void reStore(format f,precis p){
        cout.setf(f,std::ios_base::floatfield);
        cout.precision(p);
    }

    Brass::Brass(const string &fn,long an,double bal)
    :fullName(fn),acctNum(an),balance(bal){
        //成员列表
    }

    void Brass::deposit(double amt){
        if (amt < 0){
            cout<<" Negative deposit not allowed."<<" deposit canceled."<<endl;
        }
        else{
            balance += amt;
        }
    }

    void Brass::withDraw(double amt){
        //保存原始精度
        format initialStatus = setFormat();
        precis prec = cout.precision(2);

        if (amt < 0){
            cout<<" Brass::withDraw acctNum must be positive."<<" Brass::withDraw canceled."<<endl;
        }
        else if(amt <= balance){
            balance -= amt;//支出
        }else{
            //超出你的账户余额
            cout<<" Brass::withDraw acctNum of $:"<<amt<<" more than you have."<<" Brass::withDraw will be canceled."<<endl;
        }
        //恢复默认精度
        reStore(initialStatus,prec);
    }

    void Brass::viewAcct()const{
        //保存原始精度
        format initialStatus = setFormat();
        precis prec = cout.precision(2);

        cout<<" Brass::fullName:"<<fullName<<endl;
        cout<<" Brass::acctNum:"<<acctNum<<endl;
        cout<<" Brass::balance $:"<<balance<<endl;

        //恢复默认精度
        reStore(initialStatus,prec);
    }

    //余额
    double Brass::Balance()const{
        return balance;
    }


    //**********************派生类*********************
    BrassPlus::BrassPlus(const string &fn,long an,double bal ,double ml,double r )
    :Brass(fn,an,bal){
        maxLoan = ml;
        rate = r;
        owesBank = 0.0;
    }

    BrassPlus::BrassPlus(const Brass &b,double ml,double r)
    :Brass(b){
        maxLoan = ml;
        rate = r;
        owesBank = 0.0;
    }

    void BrassPlus::withDraw(double amt){
        //保存原始精度
        format initialStatus = setFormat();
        precis prec = cout.precision(2);

        double bal = Balance();
        if (amt <= bal){
            Brass::withDraw(amt);
        }else if(amt <= bal + maxLoan - owesBank){
           double advance = amt - bal;
           owesBank += advance * (1.0 + rate);
           cout<<" BrassPlus::Bank advance $:"<<advance<<endl;
           cout<<" BrassPlus::Finance charge $:"<<advance * rate<<endl;
           deposit(advance);//存款
           Brass::withDraw(amt);
        }else{
            cout<<" BrassPlus::withDraw acctNum of $:"<<amt<<" more than you have."<<"  BrassPlus::withDraw will be canceled."<<endl;
        }

        //恢复默认精度
        reStore(initialStatus,prec);
    }

    void BrassPlus::viewAcct()const{
        //保存原始精度
        format initialStatus = setFormat();
        precis prec = cout.precision(2);

        Brass::viewAcct();
        cout<<" BrassPlus::maxLoan $:"<<maxLoan<<endl;//借款
        cout<<" BrassPlus::rate:"<<rate<<endl;//利率
        cout<<" BrassPlus::owesBank $:"<<owesBank<<endl;//欠债
        //恢复默认精度
        reStore(initialStatus,prec);
    }

    void Brass::viewName()const{
         cout<<"Brass::viewName"<<endl;
    }
    void BrassPlus::viewName()const{
        cout<<"BrassPlus::viewName"<<endl;
    }
}